package com.pinda.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinda.commonutils.Page;
import com.pinda.oms.DTO.OrderDTO;
import com.pinda.oms.DTO.OrderSearchDTO;
import com.pinda.feign.UpdateStatusFeign;
import com.pinda.oms.entity.PdOrder;
import com.pinda.service.IPdOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/pd-order")
public class PdOrderController {

    @Resource
    private IPdOrderService ipOrderService;


    @ResponseBody
    @PostMapping("list")
    public List<PdOrder> list(@RequestBody OrderSearchDTO orderSearchDTO) {
        LambdaQueryWrapper<PdOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(orderSearchDTO.getStatus() != null, PdOrder::getStatus, orderSearchDTO.getStatus());
        wrapper.in(!CollectionUtils.isEmpty(orderSearchDTO.getReceiverCountyIds()), PdOrder::getReceiverCountyId, orderSearchDTO.getReceiverCountyIds());
        wrapper.in(!CollectionUtils.isEmpty(orderSearchDTO.getSenderCountyIds()), PdOrder::getSenderCountyId, orderSearchDTO.getSenderCountyIds());
        wrapper.eq(StringUtils.isNotEmpty(orderSearchDTO.getCurrentAgencyId()), PdOrder::getCurrentAgencyId, orderSearchDTO.getCurrentAgencyId());

        return ipOrderService.list(wrapper);
    }



    @PostMapping("/findOrder")
    public PageInfo<PdOrder> findOrder(@RequestBody Page page){
        System.out.println("111111");
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        List<PdOrder> list=ipOrderService.list();
        PageInfo<PdOrder> pageInfo=new PageInfo(list);
        return pageInfo;
    }

    @PostMapping("saveOrder")
    public PdOrder saveOrder(@RequestBody PdOrder pdOrder) throws Exception {

        return this.ipOrderService.saveOrder(pdOrder);
    }

    @PostMapping(value = "queryOrder/{memberId}")
    public List<PdOrder> queryOrder(@PathVariable("memberId") String memberId) {
        System.out.println(memberId);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("member_id",memberId);
        List list = this.ipOrderService.list(queryWrapper);
        System.out.println(list);
        return list;
    }

    @PostMapping(value = "queryByIdOrder/{id}")
    public PdOrder queryByIdOrder(@PathVariable("id") String id) {

        return this.ipOrderService.getById(id);
    }

    /**
     * 根据id修改
     */
    @PostMapping("/UpdOrderById/{id}")
    public boolean UpdOrderById(@PathVariable("id") String id){
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.setSql("payment_status=2");
        return ipOrderService.update(updateWrapper);
    }

    /**
     * 修改订单信息
     *
     * @param id       订单id
     * @param orderDTO 订单信息
     * @return 订单信息
     */
    @PutMapping("/{id}")
    public OrderDTO updateById(@PathVariable(name = "id") String id, @RequestBody OrderDTO orderDTO) {
        orderDTO.setId(id);
        PdOrder order = new PdOrder();
        BeanUtils.copyProperties(orderDTO, order);
        ipOrderService.updateById(order);
        return orderDTO;
    }

    /**
     * 修改收件状态为3并且修改当前订单的金额(运费)
     * @param pdOrder
     * @param id
     * @return
     */
    @PostMapping("updatafreight/{id}")
    public Boolean updatafreight(@RequestBody PdOrder pdOrder,@PathVariable("id") String id){
        return ipOrderService.updataAmountByid(pdOrder,id);
    }




    /**
     * 派送中
     * @param id
     * @return
     */
    @PostMapping("updateStatusById8/{id}")
    boolean updateStatusById8(@PathVariable("id") String id){
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.setSql("status=8");
        return ipOrderService.update(updateWrapper);
    }

    @PostMapping("updateStatusById9/{id}")
    boolean updateStatusById9(@PathVariable("id") String id){
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.setSql("status=9");
        return ipOrderService.update(updateWrapper);
    }

    /**
     * 取件 交件之后修改订单状态为网点入库（3）
     * @param id
     * @return
     */
    @GetMapping("updateStatusById3/{id}/{orgId}")
    boolean updateStatusById3(@PathVariable("id") String id,@PathVariable("orgId") String orgId){
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.setSql("status=3,current_agency_id="+orgId);
        return ipOrderService.update(updateWrapper);
    }

    @RequestMapping("inTransit")
    boolean inTransit(@RequestBody PdOrder pdOrder){
        return ipOrderService.orderUpdateByOrderId(pdOrder);
    }
}
